﻿using BySoft.Server.Business.UserCode;
using BySoft.Server.Common;
using BySoft.Server.Consts;
using BySoft.Web.Controls;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.ComPage
{
    public partial class QueryMMaterialByPurchase : BasePage
    {
        #region 变量定义
        int m_count = 0;
        private string m_strMainKey = "MaterialID";
        #endregion

        #region 属性定义

        private DataTable DtPurchaseData
        {
            get
            {
                if (null == Session["PurchaseData"])
                {
                    return CreateDT();
                }
                else
                {
                    return (DataTable)Session["PurchaseData"];
                }
            }
            set
            {
                Session["PurchaseData"] = value;
            }
        }

        private DataTable CreateDT()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("BillDetailID");//0
            dt.Columns.Add("OrderNum");//1
            dt.Columns.Add("RquisitionDetailID");//2
            dt.Columns.Add("RquisitionID");//3
            dt.Columns.Add("WorkOrderNo");//4
            dt.Columns.Add("MaterialID");//5
            dt.Columns.Add("MaterialName");//6
            dt.Columns.Add("Specifications");//7
            dt.Columns.Add("Unit");//8
            dt.Columns.Add("RquisitionNums");//需求数量
            dt.Columns.Add("ResNum");//剩余数量
            dt.Columns.Add("Nums");//11
            dt.Columns.Add("MMNumber");//11
            dt.Columns.Add("SingleMMNumber");//11
            dt.Columns.Add("Price");//12
            dt.Columns.Add("Amount");//13
            dt.Columns.Add("ExpectDate");//14
            dt.Columns.Add("IsNew");
            dt.Columns.Add("IsDel");
            return dt;
        }
        #endregion

        #region 初始化及绑定控件
        private void BindData(int pageIndex)
        {
            string IsRquisition = hdIsRquisition.Value.Trim();
            string RquisitionID = hdRquisitionID.Value.Trim();
            DataTable dt = BsPurchaseBusiness.SelectMaterial(txtMaterialID.Text.Trim(), txtMaterialName.Text.Trim(), IsRquisition, RquisitionID, pageIndex, pcData.PageSize, out m_count);
            
            pcData.RecordCount = m_count;
            if (dt.Rows.Count != 0)
            {
                grdResult.DataSource = dt;
                grdResult.DataBind();
                DataView dv = new DataView(DtPurchaseData);
                dv.RowFilter = "IsDel<>'1'";
                DataTable dtTemp = dv.ToTable();

                EnableGridViewRow(grdResult, dtTemp, m_strMainKey, false);
                pcData.CurrentIndex = pageIndex;
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, dt);
            }
            UIHelper.RunScript(this.Page, "iframeAutoFit();");
        }
        #endregion

        #region 控件事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                hdIsRquisition.Value = UIHelper.GetQueryStringValue("isrquisition");
                hdRquisitionID.Value = UIHelper.GetQueryStringValue("rid");
                InitDDL();
                grdResult.Style.Add("word-break", "keep-all");
                grdResult.Style.Add("word-wrap", "normal");
                BindData(0);
            }
        }

        private void InitDDL()
        {
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AddPro();
            BindData(0);
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            AddPro();
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>setCookie(dialogReturn_key,'success',1);CloseTopDialog();</script>");
        }

        protected void pcData_PageIndexClick(object sender, PageIndexClickEventArgs e)
        {
            AddPro();
            BindData(e.PageIndex);
        }
        #endregion

        #region 本地方法
        private void AddPro()
        {
            DtPurchaseData = GetAddPro(DtPurchaseData).Copy();
        }

        private DataTable GetAddPro(DataTable dt)
        {
            DataTable dtTemp = dt.Copy();
            foreach (GridViewRow gvr in grdResult.Rows)
            {
                if (gvr.RowType == DataControlRowType.DataRow)
                {
                    Label lblRquisitionDetailID = gvr.FindControl("lblRquisitionDetailID") as Label;
                    Label lblRquisitionID = gvr.FindControl("lblRquisitionID") as Label;
                    Label lblWorkOrderNo = gvr.FindControl("lblWorkOrderNo") as Label;
                    Label lblMaterialID = gvr.FindControl("lblMaterialID") as Label;
                    Label lblMaterialName = gvr.FindControl("lblMaterialName") as Label;
                    Label lblSpecifications = gvr.FindControl("lblSpecifications") as Label;
                    Label lblUnit = gvr.FindControl("lblUnit") as Label;
                    Label lblMMNumber = gvr.FindControl("lblMMNumber") as Label;
                    Label lblRquisitionNums = gvr.FindControl("lblRquisitionNums") as Label;
                    Label lblResNum = gvr.FindControl("lblResNum") as Label;
                    Label lblPrice = gvr.FindControl("lblPrice") as Label;

                    if (((CheckBox)gvr.FindControl("cbChoose")).Checked && gvr.Enabled)
                    {
                        DataRow[] drs = dtTemp.Select("MaterialID='" + lblMaterialID.Text.Trim() + "'  ");
                        if (drs.Length == 0)
                        {
                            DataRow dr = dtTemp.NewRow();
                            dr["RquisitionDetailID"] = lblRquisitionDetailID.Text.Trim();
                            dr["RquisitionID"] = lblRquisitionID.Text.Trim();
                            dr["WorkOrderNo"] = lblWorkOrderNo.Text.Trim();
                            dr["MaterialID"] = lblMaterialID.Text.Trim();
                            dr["MaterialName"] = lblMaterialName.Text.Trim();
                            dr["Specifications"] = lblSpecifications.Text.Trim();
                            dr["Unit"] = lblUnit.Text.Trim();
                            dr["RquisitionNums"] = lblRquisitionNums.Text.Trim();
                            dr["ResNum"] = lblResNum.Text.Trim();
                            dr["Nums"] = lblResNum.Text.Trim();
                            dr["Price"] = lblPrice.Text.Trim();
                            dr["SingleMMNumber"] = lblMMNumber.Text.Trim();
                            dr["MMNumber"] = Math.Round(CommonMethod.ConvertToDecimal(lblResNum.Text.Trim()) * CommonMethod.ConvertToDecimal(lblMMNumber.Text.Trim()), 2);
                            dr["Amount"] = Math.Round(CommonMethod.ConvertToDecimal(lblResNum.Text.Trim()) * CommonMethod.ConvertToDecimal(lblPrice.Text.Trim()), 2);
                            dr["IsDel"] = "-1";
                            dr["IsNew"] = "1";
                            dtTemp.Rows.Add(dr);
                        }
                        else if (drs.Length > 0 && CommonMethod.GetString(drs[0]["IsDel"]) == "1")
                        {
                            drs[0]["RquisitionDetailID"] = lblRquisitionDetailID.Text.Trim();
                            drs[0]["RquisitionID"] = lblRquisitionID.Text.Trim();
                            drs[0]["WorkOrderNo"] = lblWorkOrderNo.Text.Trim();
                            drs[0]["MaterialID"] = lblMaterialID.Text.Trim();
                            drs[0]["MaterialName"] = lblMaterialName.Text.Trim();
                            drs[0]["Specifications"] = lblSpecifications.Text.Trim();
                            drs[0]["Unit"] = lblUnit.Text.Trim();
                            drs[0]["RquisitionNums"] = lblRquisitionNums.Text.Trim();
                            drs[0]["ResNum"] = lblResNum.Text.Trim();
                            drs[0]["Nums"] = lblResNum.Text.Trim();
                            drs[0]["Price"] = lblPrice.Text.Trim();
                            drs[0]["SingleMMNumber"] = lblMMNumber.Text.Trim();
                            drs[0]["MMNumber"] = Math.Round(CommonMethod.ConvertToDecimal(lblResNum.Text.Trim()) * CommonMethod.ConvertToDecimal(lblMMNumber.Text.Trim()), 2);
                            drs[0]["Amount"] = Math.Round(CommonMethod.ConvertToDecimal(lblResNum.Text.Trim()) * CommonMethod.ConvertToDecimal(lblPrice.Text.Trim()), 2);
                            drs[0]["IsDel"] = "-1";
                        }
                    }
                    else
                    {
                        DataRow[] drs = dtTemp.Select("MaterialID='" + lblMaterialID.Text.Trim() + "'  ");
                        if (drs.Length > 0)
                        {
                            drs[0]["IsDel"] = "1";
                        }
                    }
                }
            }
            return dtTemp;
        }
        #endregion
    }
}
